<?php
//分账报表和复审
namespace App\Http\Controllers\Admin\Finance;

use App\Models\Finance\HistoryMusic;
use App\Models\Finance\Monthstatis;
use App\Modules\Finance\Report;
use Illuminate\Http\Request;
use App\Http\Controllers\AdapterWebController;

class ReportController extends AdapterWebController
{
    // 分账报表
    public function accountStatement(Request $request)
    {
        if($request->isMethod('get')){
            $count = Monthstatis::where('music_status','=',2)->orWhere('appoint_status','=',2)->count();
            return view('admin.finance.account_statement',['count'=>$count]);
        }
        $postData = $request->all();
        $res = $this->appLayerExec(Report::class, 'accountStatement',$postData);
        if($res['code']==200){
            return view('admin.jump')->with([
                'message'=>'计算成功！',
                'url' =>url('/admin/accountStatement'),
                'jumpTime'=>2,
            ]);
        }else{
            return auto_failure('计算失败');
        }
    }

    // 财务复审
    public function financialRetrial(Request $request)
    {
        $res = $this->appLayerExec(Report::class, 'accountsList');
        return view('admin.finance.financial_retrial', ['accounts' => $res['res'], 'type' => $request->type]);
    }

    // 历史分账记录
    public function ledgerRecord(Request $request)
    {
        $res = $this->appLayerExec(Report::class, 'accountsList');
        return view('admin.finance.report.ledger_record', ['accounts' => $res['res'], 'type' => $request->type]);
    }

    //分账明细
    public function examineBill($time, $type)
    {
        $postData['time'] = $time;
        if ($type == 'music') {
            $res = $this->appLayerExec(Report::class, 'examineBill', $postData);
            return view('admin.finance.financial_examineBill',
                ['accounts' => $res['res']['page'], 'num' => $res['res']['num'], 'time' => $time, 'sum' => $res['res']['sum']]);
        }
        if ($type == 'agent') {
            $res = $this->appLayerExec(Report::class, 'agentExamineBill', $postData);
            return view('admin.finance.financial_agentExamineBill',
                ['accounts' => $res['res']['page'], 'num' => $res['res']['num'], 'time' => $time, 'sum' => $res['res']['sum']]);
        }
        if ($type == 'appoint') {
            $res = $this->appLayerExec(Report::class, 'appointExamineBill', $postData);
            return view('admin.finance.financial_appointExamineBill',
                ['accounts' => $res['res']['page'], 'num' => $res['res']['num'], 'time' => $time, 'sum' => $res['res']['sum']]);
        }
    }

    // 重新分账
    public function reaccount()
    {
        $res = Monthstatis::where('music_status','=',2)->orWhere('appoint_status','=',2)->get()->toArray();
        return view('admin.finance.report.reaccount',['res'=>$res]);
    }

    // 拒绝分账
    public function denialOfAccounts(Request $request, $id, $type)
    {
        if ($request->isMethod('get')){
            return view('admin.finance.report.denial_of_accounts', compact('id', 'type'));
        }
        $postData['id'] = $id;
        $postData['type'] = $type;
        if ($type == 1) {
            $postData['music_reason'] = $request->reason;
            $res = $this->appLayerExec(Report::class, 'refuseApply', $postData);
            if ($res['status'] == 1) {
                flash('操作成功')->success();
                return back()->withInput();
            }
        }
        if ($type == 2) {
            $postData['appoint_reason'] = $request->reason;
            $res = $this->appLayerExec(Report::class, 'refuseApply', $postData);
            if ($res['status'] == 1) {
                flash('操作成功')->success();
                return back()->withInput();
            }
        }
    }

    public function adoptOfAccounts($id, $type)
    {
        $postData['id'] = $id;
        $postData['type'] = $type;
        $res = $this->appLayerExec(Report::class, 'adoptApply', $postData);
        if ($res['status'] == 1) {
            return [
                "error" => 0,
                "msg" => "操作成功"
            ];
        }
    }

    //搜索音乐人，代理，指定人信息
    public function typeSearch(Request $request){
        $postData=$request->all();
        $res = $this->appLayerExec(Report::class, 'typeSearch',$postData);
        return $res['res'];
    }

    //重新分账
    public function accountAgain(Request $request){
        $postData = $request->all();
        $res = $this->appLayerExec(Report::class, 'accountStatement',$postData);
        return $res;
    }

    //历史账单
    public function historyAccount(Request $request){
        if($request->isMethod('get')){
            return view('admin.finance.report.history_account');
        }
        $data = $request->all();
        if($data['type']=='history_music'){
            $res = HistoryMusic::leftJoin('cp_list',function($query){
                $query->on('history_music.title','=','cp_list.title')->on('history_music.album','=','cp_list.album')->on('history_music.composer','=','cp_list.composer');
            })->where('history_music.sp','=',$data['sp'])->get()->toArray();
            $r = count($res);
            dd($r);
        }
    }
}
